using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data;
using Fieldbook.Entities;
using Fieldbook.Common;

namespace Fieldbook.DAL
{
    public static class FieldWorkMachineDAO
    {
        #region insert
        private static DbCommand GetInsertCommand(SqlDatabase db)
        {
            string insertSQL = "INSERT INTO dbo.FieldWorkMachine (FIELDWORKID,MACHINEID,MEASUREUNITID,QUOTA) VALUES (@FIELDWORKID,@MACHINEID,@MEASUREUNITID,@QUOTA) SET @FIELDWORKMACHINEID = SCOPE_IDENTITY()";
            DbCommand insertCommand = db.GetSqlStringCommand(insertSQL);
            db.AddOutParameter(insertCommand, "@FIELDWORKMACHINEID", DbType.Int64, 8);
            db.AddInParameter(insertCommand, "@FIELDWORKID", DbType.Int64);
            db.AddInParameter(insertCommand, "@MACHINEID", DbType.Int64);            
            db.AddInParameter(insertCommand, "@MEASUREUNITID", DbType.Int32);
            db.AddInParameter(insertCommand, "@QUOTA", DbType.Decimal);
            return insertCommand;
        }
        private static void SetInsertValues(SqlDatabase db, DbCommand cmd, FieldWorkMachine entity)
        {
            db.SetParameterValue(cmd, "@FIELDWORKID", entity.FieldWorkID);
            db.SetParameterValue(cmd, "@MACHINEID", entity.MachineID);
            db.SetParameterValue(cmd, "@MEASUREUNITID", entity.MeasureUnitID);
            db.SetParameterValue(cmd, "@QUOTA", entity.Quota);
        }
        public static bool InsertFieldWorkMachine(FieldWorkMachine entity)
        {
            bool res = false;
            SqlDatabase db = new SqlDatabase(AppSettingsHelper.GetConfigValue(Constants.FieldbookConnectionString));
            DbCommand cmd = GetInsertCommand(db);
            SetInsertValues(db, cmd, entity);
            res = Convert.ToBoolean(db.ExecuteNonQuery(cmd));
            if (res)
            {
                entity.FieldWorkMachineID = (long?)db.GetParameterValue(cmd, "@FIELDWORKMACHINEID");
            }
            return res;
        }
        public static bool InsertFieldWorkMachine(FieldWorkMachine entity, DbTransaction transactionObj, SqlDatabase db)
        {
            bool res = false;
            DbCommand cmd = GetInsertCommand(db);
            SetInsertValues(db, cmd, entity);
            res = Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
            if (res)
            {
                entity.FieldWorkMachineID = (long?)db.GetParameterValue(cmd, "@FIELDWORKMACHINEID");
            }
            return res;
        }
        public static bool InsertListFieldWorkMachine(List<FieldWorkMachine> list, DbTransaction transactionObj, SqlDatabase db)
        {
            bool res = true;
            DbCommand cmd = GetInsertCommand(db);
            for (int i = 0; i < list.Count; i++)
            {
                FieldWorkMachine entity = list[i];
                SetInsertValues(db, cmd, entity);
                res = res & Convert.ToBoolean(db.ExecuteNonQuery(cmd, transactionObj));
                if (res)
                {
                    entity.FieldWorkMachineID = (long?)db.GetParameterValue(cmd, "@FIELDWORKMACHINEID");
                }
            }
            return res;
        }
        #endregion
    }
}
